From 6633e2902ffdd14cb408792eebaa62ee740e936b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 4 May 2017 15:23:38 +0300 Subject: [PATCH] Blacklist some binary names in `cargo new` --- src/cargo/ops/cargo_new.rs | 9 +++++---- src/cargo/ops/cargo_rustc/layout.rs | 7 +++++++ src/cargo/ops/cargo_rustc/mod.rs | 1 + src/cargo/ops/mod.rs | 2 +- src/cargo/util/toml.rs | 5 ++--- tests/bench.rs | 1 - tests/new.rs | 9 +++++++++ 7 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index 195c6efaf..5b4f4faae 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -10,6 +10,7 @@ use git2::Config as GitConfig; use term::color::BLACK; use core::Workspace; +use ops::is_bad_artifact_name; use util::{GitRepo, HgRepo, PijulRepo, CargoResult, human, ChainError, internal}; use util::{Config, paths}; @@ -115,7 +116,7 @@ fn get_name<'a>(path: &'a Path, opts: &'a NewOptions, config: &Config) -> CargoR } } -fn check_name(name: &str) -> CargoResult<()> { +fn check_name(name: &str, is_bin: bool) -> CargoResult<()> { // Ban keywords + test list found at // https://doc.rust-lang.org/grammar.html#keywords @@ -130,7 +131,7 @@ fn check_name(name: &str) -> CargoResult<()> { "super", "test", "trait", "true", "type", "typeof", "unsafe", "unsized", "use", "virtual", "where", "while", "yield"]; - if blacklist.contains(&name) { + if blacklist.contains(&name) || (is_bin && is_bad_artifact_name(name)) { bail!("The name `{}` cannot be used as a crate name\n\ use --name to override crate name", name) @@ -274,7 +275,7 @@ pub fn new(opts: NewOptions, config: &Config) -> CargoResult<()> { } let name = get_name(&path, &opts, config)?; - check_name(name)?; + check_name(name, opts.bin)?; let mkopts = MkOptions { version_control: opts.version_control, @@ -303,7 +304,7 @@ pub fn init(opts: NewOptions, config: &Config) -> CargoResult<()> { } let name = get_name(&path, &opts, config)?; - check_name(name)?; + check_name(name, opts.bin)?; let mut src_paths_types = vec![]; diff --git a/src/cargo/ops/cargo_rustc/layout.rs b/src/cargo/ops/cargo_rustc/layout.rs index f090cbc72..66882e628 100644 --- a/src/cargo/ops/cargo_rustc/layout.rs +++ b/src/cargo/ops/cargo_rustc/layout.rs @@ -67,6 +67,13 @@ pub struct Layout { _lock: FileLock, } +pub fn is_bad_artifact_name(name: &str) -> bool { + ["deps", "examples", "build", "native", "incremental"] + .iter() + .find(|&&reserved| reserved == name) + .is_some() +} + impl Layout { pub fn new(ws: &Workspace, triple: Option<&str>, diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index ae3af71a6..f52f86860 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -23,6 +23,7 @@ use self::output_depinfo::output_depinfo; pub use self::compilation::Compilation; pub use self::context::{Context, Unit}; pub use self::custom_build::{BuildOutput, BuildMap, BuildScripts}; +pub use self::layout::is_bad_artifact_name; mod compilation; mod context; diff --git a/src/cargo/ops/mod.rs b/src/cargo/ops/mod.rs index 9ec74f393..8d8908320 100644 --- a/src/cargo/ops/mod.rs +++ b/src/cargo/ops/mod.rs @@ -3,7 +3,7 @@ pub use self::cargo_compile::{compile, compile_with_exec, compile_ws, CompileOpt pub use self::cargo_compile::{CompileFilter, CompileMode, MessageFormat, Packages}; pub use self::cargo_read_manifest::{read_manifest,read_package,read_packages}; pub use self::cargo_rustc::{compile_targets, Compilation, Kind, Unit}; -pub use self::cargo_rustc::Context; +pub use self::cargo_rustc::{Context, is_bad_artifact_name}; pub use self::cargo_rustc::{BuildOutput, BuildConfig, TargetConfig}; pub use self::cargo_rustc::{Executor, DefaultExecutor}; pub use self::cargo_run::run; diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 24006bf22..771f48ddf 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -15,6 +15,7 @@ use core::{Summary, Manifest, Target, Dependency, DependencyInner, PackageId}; use core::{EitherManifest, VirtualManifest}; use core::dependency::{Kind, Platform}; use core::manifest::{LibKind, Profile, ManifestMetadata}; +use ops::is_bad_artifact_name; use sources::CRATES_IO; use util::{self, CargoResult, human, ToUrl, ToSemver, ChainError, Config}; @@ -613,10 +614,8 @@ impl TomlManifest { None => inferred_bin_targets(&project.name, layout) }; - let blacklist = vec!["build", "deps", "examples", "native"]; - for bin in bins.iter() { - if blacklist.iter().find(|&x| *x == bin.name()) != None { + if is_bad_artifact_name(&bin.name()) { bail!("the binary target name `{}` is forbidden", bin.name()) } diff --git a/tests/bench.rs b/tests/bench.rs index 33a6529b7..f22ec077c 100644 --- a/tests/bench.rs +++ b/tests/bench.rs @@ -7,7 +7,6 @@ use std::str; use cargo::util::process; use cargotest::is_nightly; use cargotest::support::paths::CargoPathExt; -use cargotest::support::registry::Package; use cargotest::support::{project, execs, basic_bin_manifest, basic_lib_manifest}; use hamcrest::{assert_that, existing_file}; diff --git a/tests/new.rs b/tests/new.rs index a2a3af453..0312d7795 100644 --- a/tests/new.rs +++ b/tests/new.rs @@ -134,6 +134,15 @@ fn reserved_name() { use --name to override crate name")); } +#[test] +fn reserved_binary_name() { + assert_that(cargo_process("new").arg("--bin").arg("incremental"), + execs().with_status(101) + .with_stderr("\ +[ERROR] The name `incremental` cannot be used as a crate name\n\ +use --name to override crate name")); +} + #[test] fn keyword_name() { assert_that(cargo_process("new").arg("pub"), -- 2.30.2